package com.nfts.imagepicker.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.nfts.imagepicker.R;
import com.nfts.imagepicker.util.ImageLoader;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by 9527 on 2016/1/3.
 */
public class ImageAapter extends BaseAdapter {
    private String mDirPath;
    private List<String> mImagePaths;
    private LayoutInflater mInflater;
    private static Set<String> mSelectedImage = new HashSet<>();
private Context mContext;


    public ImageAapter(Context context, List<String> mDatas, String dirPath) {
        mDirPath = dirPath;
        mImagePaths = mDatas;
        mInflater = LayoutInflater.from(context);
mContext = context;
    }

    @Override
    public int getCount() {
        return mImagePaths.size();
    }

    @Override
    public Object getItem(int position) {
        return mImagePaths.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_gridview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.image_item_gridview);
            viewHolder.mImageButton = (ImageButton) convertView.findViewById(R.id.select_ib_item_gridview);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//重置状态
        viewHolder.mImageView.setImageResource(R.drawable.pictures_no);
        viewHolder.mImageView.setColorFilter(null);
        viewHolder.mImageButton.setImageResource(R.drawable.picture_unselected);
        viewHolder.mImageView.setMaxWidth(300);


        ImageLoader.getInstance(3,ImageLoader.Type.LIFO).loadImage(mDirPath + "/" + mImagePaths.get(position), viewHolder.mImageView);
//        Picasso.with(mContext).setLoggingEnabled(true);
//        Picasso.with(mContext).load(new File(mDirPath + "/" + mImagePaths.get(position))).resize(100,100).into(viewHolder.mImageView);
        final String filePath = mDirPath + "/" + mImagePaths.get(position);
        viewHolder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mSelectedImage.contains(filePath)) {//已经被选中
                    mSelectedImage.remove(filePath);
                    viewHolder.mImageView.setColorFilter(null);
                    viewHolder.mImageButton.setImageResource(R.drawable.picture_unselected);
                } else {
                    mSelectedImage.add(filePath);
                    viewHolder.mImageView.setColorFilter(Color.parseColor("#77000000"));
                    viewHolder.mImageButton.setImageResource(R.drawable.pictures_selected);
                }

            }
        });

        if (mSelectedImage.contains(filePath)){
            viewHolder.mImageView.setColorFilter(Color.parseColor("#77000000"));
            viewHolder.mImageButton.setImageResource(R.drawable.pictures_selected);
        }

        return convertView;
    }

    private class ViewHolder {
        ImageView mImageView;
        ImageButton mImageButton;
    }
}